package com.zhiyin.seal.service.impl;

import com.zhiyin.seal.dao.ApprovalDao;
import com.zhiyin.seal.entity.ApprovalCCEntity;
import com.zhiyin.seal.entity.ApprovalProcessEntity;
import com.zhiyin.seal.entity.ApprovalUserEntity;
import com.zhiyin.seal.entity.bo.*;
import com.zhiyin.seal.service.ApprovalService;
import com.zhiyin.seal.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApprovalServiceImpl implements ApprovalService {

    @Autowired
    ApprovalDao approvalDao;

    @Override
    public ApprovalProcessBo getApprovalProcessById(Long processId) {
        return BeanUtil.copy(approvalDao.selectApprovalProcessById(processId),ApprovalProcessBo.class);
    }

    @Override
    public List<ApprovalProcessBo> getApprovalProcessList(Long companyId) {
        return BeanUtil.copyList(approvalDao.selectApprovalProcessList(companyId),ApprovalProcessBo.class);
    }

    @Override
    public List<ApprovalProcessBo> getApprovalProcessListBySealId(Long sealId) {
        return BeanUtil.copyList(approvalDao.selectApprovalProcessListBySealId(sealId),ApprovalProcessBo.class);
    }

    @Override
    public Boolean updateApprovalProcess(ApprovalProcessBo approvalProcessBo) {
        return approvalDao.updateApprovalProcess(BeanUtil.copy(approvalProcessBo, ApprovalProcessEntity.class)) > 0;
    }

    @Override
    public Boolean removeApprovalProcess(Long processId) {
        return approvalDao.deleteApprovalProcess(processId) > 0;
    }

    @Override
    public Boolean removeCompanyApprovalProcess(Long companyId) {
        return approvalDao.deleteCompanyApprovalProcess(companyId) > 0;
    }

    @Override
    public List<ApprovalUserBo> getApprovalUserList(Long processId) {
        return BeanUtil.copyList(approvalDao.selectApprovalUserList(processId),ApprovalUserBo.class);
    }

    @Override
    public List<ApprovalCCBo> getApprovalCCList(Long processId) {
        return BeanUtil.copyList(approvalDao.selectApprovalCCList(processId),ApprovalCCBo.class);
    }

    @Override
    public Long addApprovalProcess(ApprovalProcessBo approvalProcessBo) {
        ApprovalProcessEntity approvalProcessEntity = BeanUtil.copy(approvalProcessBo,ApprovalProcessEntity.class);
        approvalDao.insertApprovalProcess(approvalProcessEntity);
        return approvalProcessEntity.getId();
    }

    @Override
    public Long getApprovalProcessId(Long sealId, List<Long> departmentIds, Long companyId) {
        return approvalDao.selectApprovalProcessId(sealId,departmentIds,companyId);
    }

    @Override
    public Boolean addApprovalUserList(Long processId,List<ApprovalUserBo> approvalUserBos) {
        List<ApprovalUserEntity> approvalUserEntityList = BeanUtil.copyList(approvalUserBos, ApprovalUserEntity.class);
        return approvalDao.insertApprovalUserList(processId,approvalUserEntityList) > 0;
    }

    @Override
    public Boolean addApprovalCCList(Long processId,List<ApprovalCCBo> approvalCCBoList) {
        return approvalDao.insertApprovalCCList(processId,BeanUtil.copyList(approvalCCBoList, ApprovalCCEntity.class)) > 0;
    }

    @Override
    public Boolean removeApprovalUserList(Long processId) {
        return approvalDao.deleteApprovalUserList(processId) > 0;
    }

    @Override
    public Boolean removeApprovalUser(Long userId, Long companyId) {
        return approvalDao.deleteApprovalUser(userId,companyId) > 0;
    }

    @Override
    public Boolean removeApprovalCCList(Long processId) {
        return approvalDao.deleteApprovalCCList(processId) > 0;
    }

    @Override
    public Boolean removeApprovalCC(Long userId, Long companyId) {
        return approvalDao.deleteApprovalCC(userId,companyId) > 0;
    }

    @Override
    public Boolean removeApprovalProcessBySealId(Long sealId) {
        return approvalDao.deleteApprovalProcessBySealId(sealId) > 0;
    }

    @Override
    public Boolean getHasApprovalProcess(Long sealId, Long departmentId, Long companyId) {
        return approvalDao.selectHasApprovalProcess(sealId,departmentId,companyId) > 0;
    }

    @Override
    public ApprovalProcessBo getApprovalProcessByNameSealIdCompanyId(String name, Long sealId, Long companyId) {
        return BeanUtil.copy(approvalDao.selectApprovalProcessByNameSealIdCompanyId(name,sealId,companyId),ApprovalProcessBo.class);
    }
}